package com.rc.evcharger.client;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rc.evcharger.common.base.TResponse;
import com.rc.evcharger.common.config.RouterConfig;
import com.rc.evcharger.common.constant.SocketConstant;
import com.rc.evcharger.model.elec.ElecPileHost;
import com.rc.evcharger.service.elec.ElecPileHostService;
import com.rc.evcharger.vo.charge.RemoteUpdateReq;
import com.rc.evcharger.utils.RestTemplateUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/**
 * Create by Yang on 2021/7/26
 */
@Component
public class CommandClientRouter {
    private static Logger logger = LogManager.getLogger(CommandClientRouter.class);

    @Autowired
    private ElecPileHostService elecPileHostService;

    @Autowired
    private RestTemplateUtil restTemplateUtil;


    public TResponse remoteUpdate(Long pileId, String fileName, String startType) {
        //查地址表
        ElecPileHost elecPileHost = elecPileHostService.selectByPrimaryKey(pileId);

        //拼接eureka访问地址
        String requestUrl = RouterConfig.CMD_REMOTE_UPDATE;
        if (SocketConstant.STD_DOMAIN.equals(elecPileHost.getElecServerDomain())) {
            requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.STD_SERVICE_PATH + RouterConfig.CMD_REMOTE_UPDATE;
        } else {
            return TResponse.FAIL("暂无非标桩的远程升级功能");
        }

        RemoteUpdateReq remoteUpdateReq = new RemoteUpdateReq();
        remoteUpdateReq.setPileEpNo(elecPileHost.getPileNo());
        remoteUpdateReq.setFileName(fileName);
        remoteUpdateReq.setStartType(startType);
        remoteUpdateReq.setPileId(elecPileHost.getElecPileId());

        ObjectMapper objectMapper = new ObjectMapper();
        String respData = null;
        try {
            respData = objectMapper.writeValueAsString(remoteUpdateReq);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        JSONObject tRequestJson = JSONObject.parseObject(respData);

        logger.info("std入参加密接口,远程升级功能被调用，...............对应url:{},orderSource:{}", requestUrl);
        //通过rest调用远程服务接口
        ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, tRequestJson, TResponse.class);
        logger.info("std入参加密接口,远程升级功能被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
        return request.getBody();
    }


    /**
     * 远程查询桩实时功率
     *
     * @param pileId
     * @return
     */

    public TResponse sendFindElecPilePower(Long pileId) {
        //查地址表
        ElecPileHost elecPileHost = elecPileHostService.selectByPrimaryKey(pileId);

        //拼接eureka访问地址
        String requestUrl = RouterConfig.REMOTE_FIND_POWE_PATH;
        if (SocketConstant.STD_DOMAIN.equals(elecPileHost.getElecServerDomain())) {
            requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.STD_SERVICE_PATH + RouterConfig.REMOTE_FIND_POWE_PATH;
        } else {
            return TResponse.FAIL("暂无远程查询桩实时功率:" + pileId);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pileId", pileId);
        logger.info("std入参加密接口,远程查询桩实时功率被调用，...............对应url:{},orderSource:{}", requestUrl);
        //通过rest调用远程服务接口
        ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, jsonObject, TResponse.class);
        logger.info("std入参加密接口,远程查询桩实时功率被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
        return request.getBody();
    }


    /**
     * 远程更新桩功率
     *
     * @param pileId
     * @return
     */

    public TResponse sendUpdateElecPilePower(Long pileId, BigDecimal power) {
        //查地址表
        ElecPileHost elecPileHost = elecPileHostService.selectByPrimaryKey(pileId);

        //拼接eureka访问地址
        String requestUrl = RouterConfig.REMOTE_UDPADE_POWE_PATH;
        if (SocketConstant.STD_DOMAIN.equals(elecPileHost.getElecServerDomain())) {
            requestUrl = "http://" + elecPileHost.getApplicationName() + RouterConfig.STD_SERVICE_PATH + RouterConfig.REMOTE_UDPADE_POWE_PATH;
        } else {
            return TResponse.FAIL("暂无远程更新桩功率:" + pileId);
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("pileId", pileId);
        jsonObject.put("power", power);
        logger.info("std入参加密接口,远程更新桩功率被调用，...............对应url:{},orderSource:{}", requestUrl);
        //通过rest调用远程服务接口
        ResponseEntity<TResponse> request = restTemplateUtil.request(requestUrl, HttpMethod.POST, jsonObject, TResponse.class);
        logger.info("std入参加密接口,远程更新桩功率被调用，...............返回状态码:{},结果集:{}", request.getStatusCode(), request.getBody().toString());
        return request.getBody();
    }
}
